<?php
// 本类由系统自动生成，仅供测试用途
class AccountAction extends Action {
	//private $_uid = '';
	//public function _initialize(){
	//	$this->_uid = session('uid');
	//	$data = array(
	//		''	
	//	);
	//	if(!$this->_uid) $this->display("Public:login");//(503,"对不起，您登录超时了，请重新登录！",1);
		//B('Authenticate', $action);
	//}
    public function index(){
		$Assets = M('Assets');
		$as = $Assets->where('uid = 8')->find();
		dump($as);
		$this->display();
    }
	
	/*======================================================================*\
		Function:	accountInfo
		Purpose:	账号信息查看与修改
		Input:		$params			        参数
		Output:		模板输出		    	#
	\*======================================================================*/
	public function accountInfo(){
		$Account = M('Account');
		$post = $this->_param();
		$uid = $post['uid'];
		if(trim($_POST['company_name']) == ''){
			
			$data = $Account->where("uid = $uid")->find();
		
			$result = array(
				'uid' => $data['uid'],
				'account'	=> $data['account'],
				'company_name' => $data['company_name'],
				'company_contacter' => $data['company_contacter'],
				'contact_telphone' => $data['contact_telphone'],
				'contact_mobilephone' => $data['contact_mobilephone'],
				'industry' => $data['industry'],
				'customer_manager' => $data['customer_manager'],
				'company_fax' => $data['company_fax'],
				'company_position' => $data['company_position'],
				'zip_code' => $data['zip_code'],
				'email' => $data['email'],
				'company_address' => $data['company_address'],
				'remark'	=> $data['remark']
			);
		
			$this->assign('list',$result);
			$this->display();
		}else{
			$result = array(
				'uid' => $uid,
				'company_name' => $post['company_name'],
				'company_contacter' => $post['company_contacter'],
				'contact_telphone' => $post['contact_telphone'],
				'contact_mobilephone' => $post['contact_mobilephone'],
				'industry' => $post['industry'],
				'customer_manager' => $post['customer_manager'],
				'company_fax' => $post['company_fax'],
				'company_position' => $post['company_position'],
				'zip_code' => $post['zip_code'],
				'email' => $post['email'],
				'company_address' => $post['company_address'],
				'remark'	=> $post['remark'],
				'update_time' => datetime()
			);
			
			if($Account->save($result)){
				logs($uid,'账号信息修改保存成功！',1);
				$this->ajaxReturn(1,"保存成功！",1);
			}else{
				logs($uid,'账号信息修改保存失败！',0);
				$this->ajaxReturn(0,"保存失败！",0);
			}

			
		}	
	}
	public function accountManage(){
		$Assets = M('Assets');
		$Account = M('Account');
		import('ORG.Util.Page');// 导入分页类
		$count      = $Account->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$param = $this->_param();
		$where = array();
		$param['account'] ? $where['account'] = $param['account']:1;
		$data = $Account->field('uid,account,company_name,company_contacter,update_time,status,contact_mobilephone')->where($where)->order('update_time create_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		//echo $Account->getLastSql();//打印sql语句
		$result = array();
		$icount = count($data);
		for($i = 0;$i < $icount;$i++){
			$data[$i]['assets'] = $Assets->where("uid = ".$data[$i]['uid'])->getField('assets_total');
			$data[$i]['assets'] ? 1 : $data[$i]['assets'] = '0.00'; 
		}
		//dump($data);
		$this->assign('list',$data);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	public function checkAccount(){
		$GET = $this->_get();
		if($GET['account']){
			$Account = M('Account');
			
			$data['account'] = trim($GET['account']);
			$is = $Account->where($data)->find();
			if($is){
				logs($uid,'注册失败：该账号已经注册',0,$GET['account']);
				$this->ajaxReturn(1,"该账号已经注册",1);

			}else{
				logs($uid,'注册失败：该账号可以注册',1,$GET['account']);
				$this->ajaxReturn(0,"该账号可以注册",0);
			}
		}
	}
	//修改密码
	public function resetPassword(){
		$get = $this->_get();
		$uid = session('uid');
		if(!$uid) $this->redirect('Public:login');
		$Account = M('Account');
		if($get['password']==''){
			$this->error("请输入密码");
		}else if($get['password'] != $get['repassword']){
			logs($uid,'修改密码：两次输入的密码不对',0);
			$this->error("两次输入的密码不对!");			
		}else{
			
			$data = array(
					'uid'      => $uid,
					'password' => $get['password']
			);
			if($Account->save($data)) logs($uid,'修改密码：成功',1);
			else logs($uid,'修改密码：失败',0);
		}
	}
	//公司签名修改
	public function signatureEdit(){
		$get = $this->_get();
		$Account = M('Account');
		if($get['signature']==''){
			$this->ajaxReturn(0,"请输入密码",0);
		}else{
			if(strlen($get['signature'] > 20)) $this->ajaxReturn(0,"签名不能超过20个字符",0);
			$data = array(
					'uid'      => $uid,
					'company_signature' => $get['signature']
			);
			$v = $Account->save($data);
			if(!$v){
				 logs($uid,'更新签名失败',0);
				$this->ajaxReturn(0,"更新签名失败",0);
			}else{
				logs($uid,'更新签名成功',1);
				$this->ajaxReturn(1,"更新签名成功",1);
			}
					
		}
	
	}

	/**
	 * 账号充值
	 * @param   type    $varname    description
	 * @return  type    description
	 * @access  public or private
	 * @static  makes the class property accessible without needing an instantiation of the class
	 */
	public 	function charge(){	    
		$Account = M('Account');
		$Assets = M('Assets');
		$Parameters = M('Parameters');
		$param = $this->_param();
		if($param['charge_password']){
			//判断充值密码
			$chargePwd = $Parameters->where("name = 'charge_password'")->getField('value');
			
			if($param['charge_password'] == $chargePwd){
				
				$data = array(
					'uid' => $param['charge_uid'],
				);
				//$Assets->where()->setInc("assets_total",)
				$order_id = $this->_chargeOrder($param['charge_uid'],$param['charge_total'],$param['charge_remark']);
				if($order_id){
					$v = __balance($param['charge_uid'],$order_id,'charge',$param['charge_total']);
					if($v){
						//充值成功
						logs($param['charge_uid'],'充值成功',1,$param['charge_total']);
						$this->ajaxReturn(1,"充值成功",1);
					}else{
						//charge fail
						logs($param['charge_uid'],"充值失败".$param['charge_uid'],0);
						$this->ajaxReturn(2,"充值失败".$param['charge_uid'],0);
					}
				}else{
					//加入充值订单失败
					logs($param['charge_uid'],"加入充值订单失败",0);
					$this->ajaxReturn(3,"加入充值订单失败",0);
				}
			}else{
				logs($param['charge_uid'],"充值密码不对",0);
				$this->ajaxReturn(4,"充值密码不对",0);
			}
			$this->ajaxReturn(5,"充值失败",0);
		}else{
			//展示
			$account = $Account->field('sms_account.uid,account,assets_total')->join('sms_assets on sms_assets.uid = sms_account.uid')->where("sms_account.uid = ".$param['uid'])->find();
			$account['assets_total'] ? 1 : $account['assets_total'] = '0.00';
			$this->assign('list',$account);
			$this->display();
		}
		
	} // end func

	
	/**
	 * Short description.
	 * @param   type    $varname    description
	 * @return  type    description
	 * @access  public or private
	 * @static  makes the class property accessible without needing an instantiation of the class
	 */
	protected function _chargeOrder($uid,$amount,$remark){
		$ChargeOrder = M('ChargeOrder');
		$Assets = M('Assets');
		$assets = $Assets->where("uid = $uid")->find();
		$handleUid = $this->_uid;
		$assets = $assets['assets_total'] + $amount;
		$data = array(
			'uid' => $uid,
			'order_number' => geneOrderNumber(),  //订单号
			'order_amount' => $amount,  
			'pay_amount'   => $amount, //充值金额
			'assets'	   => $assets,	       //目前总资产
			'handler_uid' => $handleUid,
			'user_remark' => '',
			'handler_remark' => $remark ? $remark : 'admin user direct charge', //'admin user direct charge',
			'create_time' => datetime(),
			'update_time' => '' ,
			'handle_time' => datetime(),
			'type'		  => 1,//后台人员直接充值，不经过用户提交充值订单
			'status'	  => 1
		);
		$v = $ChargeOrder->add($data);
		if($v){
			logs($uid,'添加到订单成功',$v,1);
			return $v; //返回订单ID
		}else{
			logs($uid,'添加到订单失败','',0);
			return false;
		}
	} // end func

	

	public function consumeRecords(){
		$Model = M();
		$param = $this->_param();
		//dump($param);
		$where = array();
		$param['account'] ? $where['B.account'] = $param['account']:1;
		$param['startdate'] ? $where['A.create_time'][] = array('EGT',$param['startdate']):1;//注意区间查询一定要这样哦
		$param['enddate'] ? $where['A.create_time'][] = array('ELT',$param['enddate']):1;
		import('ORG.Util.Page');// 导入分页类
		$count      = $Model->table('sms_consume_order A')->join('sms_account B ON A.owner_uid = B.uid')->field('A.*,B.uid,B.account,B.company_name,B.company_contacter')->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		
		$data = $Model->table('sms_consume_order A')->join('sms_account B ON A.owner_uid = B.uid')->field('A.*,B.uid,B.account,B.company_name,B.company_contacter')->where($where)->order('A.create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		//echo $Model->getLastSql();//打印sql语句
		//$icount = count($data);
		/*
		for($i = 0;$i < $icount;$i++){				
			$data[$i]['total'] ? 1 : $data[$i]['phone_total'] = $data[$i]['sms_total'];
		}
		*/
		//dump($data);
		$this->assign('list',$data);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	//账户充值记录
	//
	public function chargeRecords(){
		$Account = M('Account');
		$Model = M();
		$param = $this->_param();
		$where = array();
		$param['account'] ? $where['B.account'] = $param['account']:1;
		$param['startdate'] ? $where['A.create_time'][] = array('EGT',$param['startdate']):1;//注意区间查询一定要这样哦
		$param['enddate'] ? $where['A.create_time'][] = array('ELT',$param['enddate']):1;
		import('ORG.Util.Page');// 导入分页类
		$count      = $Model->table('sms_charge_order A')->join('sms_account B ON A.uid = B.uid')->field('A.*,B.uid,B.account,B.company_name,B.company_contacter')->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		

		$data = $Model->table('sms_charge_order A')->join('sms_account B ON A.uid = B.uid')->field('A.*,B.uid,B.account,B.company_name,B.company_contacter')->where($where)->order('A.handle_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		//echo $Model->getLastSql();//打印sql语句
		$result = array();
		$icount = count($data);
		for($i = 0;$i < $icount;$i++){
			$ac = $Account->where("uid = ".$data[$i]['uid'])->find();
			$data[$i]['account'] = $ac['account'];
			$data[$i]['company_name'] = $ac['company_name'];
		}
		//dump($data);
		$this->assign('list',$data);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	//企业签名
	public function signature(){
		
		$this->display();
	}
	//订单记录
	public function orderRecords(){
		$Order = M('Order');
		$Account = M('Account');
		import('ORG.Util.Page');// 导入分页类
		$count      = $Order->count();// 查询满足要求的总记录数
		$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$data = $Order->order('update_time')->limit($Page->firstRow.','.$Page->listRows)->select();
		$result = array();
		$icount = count($data);
		for($i = 0;$i < $icount;$i++){
			$ac = $Account->where("uid = ".$data[$i]['uid'])->find();
			$data[$i]['account'] = $ac['account'];
			$data[$i]['company_name'] = $ac['company_name'];
		}
		//dump($data);
		$this->assign('list',$data);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	//订单审核
	public function orderCheck(){
		$Order = M('Order');
		$Account = M('Account');
		import('ORG.Util.Page');// 导入分页类
		$count      = $Order->where('status = 0')->count();// 查询满足要求的总记录数
		$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$data = $Order->where('status = 0')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
		$result = array();
		foreach($data as $value){
			$ac = $Account->where("uid = ".$value['uid'])->find();
			
			$list[] = array(
				'order_id' => $value['order_id'],
				'uid' => $value['uid'],
				'account' =>$ac['account'],
				'company_name' =>$ac['company_name'],
				'order_amount' =>$value['order_amount'],
				'create_time'  => $value['create_time'],
				'user_remark'  => $value['user_remark']
			);
		}dump($list);
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	//订单确认
	public function orderConfirm(){
		$Order = M('Order');
		$Account = M('Account');
		$post = $this->_post();
		$data = array(
			'pay_amount' => $post['pay_amount'],
			'handler_id' => $this->_uid,
			'handle_time'=> date("Y-m-d H:i:s"),
			'handler_remark' => $post['handler_remark']
		);
		$v = $Order->save($data);
		if($v){
			logs($uid,"确认订单成功",1);
			$this->ajaxReturn(1,"确认订单成功",1);
		}else{
			logs($uid,"确认订单失败",0);
			$this->ajaxReturn(0,"确认订单失败",0);
		}
	}
	
}